//
//  LCReadDetailHostViewController.m
//  YinYueTai
//
//  Created by CongCong on 15-7-18.
//  Copyright (c) 2015年 CongCong. All rights reserved.
//

#import "LCReadDetailHostViewController.h"

@interface LCReadDetailHostViewController ()
@property (nonatomic, strong) LCReadNewAndHotModel *hotModel;
@end

@implementation LCReadDetailHostViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatCollection];
    [self registerNib];
    [self creatRefresh];
    [self fetchWebDataWith:kReaderListurlHeader body:[NSString stringWithFormat:kReaderListHotRrlBody,self.page,self.type.integerValue]];
    
}

- (void)registerNib
{
    self.collectionView.frame = CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-104);
    self.flowLayout.minimumLineSpacing = 2;
    self.flowLayout.minimumInteritemSpacing = 2;
    [self.collectionView registerNib:[UINib nibWithNibName:@"LCReadHotAndNewCell" bundle:nil] forCellWithReuseIdentifier:@"LCReadHotAndNewCell"];
}
- (void)creatRefresh
{
    __weak typeof(self) weakSelf = self;
    [self.collectionView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        weakSelf.page = 0;
        [weakSelf fetchWebDataWith:kReaderListurlHeader body:[NSString stringWithFormat:kReaderListNewUrlBody,weakSelf.page,weakSelf.type.integerValue]];
    }];
    [self.collectionView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        weakSelf.page+=10;
        if (weakSelf.page>=weakSelf.hotModel.total.integerValue) {
            [MMProgressHUD show];
            [MMProgressHUD  dismissWithSuccess:@"到底了..."];
            [weakSelf.collectionView footerEndRefreshing];
            return ;
        }
        else
        {
           [weakSelf fetchWebDataWith:kReaderListurlHeader body:[NSString stringWithFormat:kReaderListNewUrlBody,weakSelf.page,weakSelf.type.integerValue]];
        }
    }];
}
- (void)initJosnModelWith:(NSDictionary *)dataDict
{
    NSDictionary *dict = dataDict[@"data"];
    if (self.page==0) {
        self.hotModel = [[LCReadNewAndHotModel alloc]initWithDictionary:dict error:nil];
    }
    else
    {
        LCReadNewAndHotModel *model = [[LCReadNewAndHotModel alloc]initWithDictionary:dict error:nil];
        [self.hotModel.list addObjectsFromArray:model.list];
    }
    [self.collectionView reloadData];
}

#pragma mark - <UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.hotModel.list.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCReadHotAndNewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LCReadHotAndNewCell" forIndexPath:indexPath];
    LCNewAndHotListModel *model = self.hotModel.list[indexPath.row];
    [cell showModel:model];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(kScreenSize.width-10, 100);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //点击事件
    LCReadTextController *text = [[LCReadTextController alloc]init];
    LCNewAndHotListModel *model = self.hotModel.list[indexPath.row];
    text.textID = model.id;
    text.title = model.title;
    [self.navigationController pushViewController:text animated:YES];
    
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *header;
    if([kind isEqual:UICollectionElementKindSectionHeader])
    {
        header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Header" forIndexPath:indexPath];
        
    }
    return header;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeMake(0, 0);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
